//
//  ZJStudyCourseSynopsisTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/6/1.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJStudyCourseSynopsisTableViewCell.h"
#import "ZJSCSynopsisSectionView.h"
#import "ZJCourseGradeView.h"
#define CellID @"ZJSCSynopsisTableViewCell"
#define SectionID @"ZJSCSynopsisSectionView"
@interface ZJStudyCourseSynopsisTableViewCell ()<UITableViewDelegate,UITableViewDataSource,ZJCourseGradeViewDelegate>
{
    
}
/**  课程点击量 */
@property (nonatomic, strong) UIButton *clickNumber;

/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 评分 */
@property (nonatomic, strong) ZJCourseGradeView *gradeView;
@end

@implementation ZJStudyCourseSynopsisTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.tableView];
        
        self.tableView.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 0)
        .rightSpaceToView(self.contentView, 0)
        .bottomSpaceToView(self.contentView, 0);

    }
    return self;
}

#pragma mark - ZJCourseGradeViewDelegate
- (void)ZJCourseGradeViewCommentNumber:(NSString *)number{
    if (self.delegate) {
        [self.delegate ZJStudyCourseSynopsisTableViewCellWithNumber:number];
    }

}

#pragma mark - UITableView 协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (!_model) {
        return 0;
    }
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJSCSynopsisTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID forIndexPath:indexPath];
    
    if (indexPath.section == 1) {
        cell.isSynopsisIntroduce = YES;
    }else{
        cell.isSynopsisIntroduce = NO;
    }
    cell.model = self.model;
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ZJSCSynopsisSectionView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionID];
    if (section == 0) {
        view.titleLabel.text = @"来源 :";
    }else{
        view.titleLabel.text = @"课程简介 :";
    }
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        self.model.isSynopsisIntroduce = YES;
    }else{
        self.model.isSynopsisIntroduce = NO;
    }

    return self.model.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  44 * [UIScreen zj_screenHScale];
}




#pragma mark - Setter
- (void)setModel:(ZJSCSynopsisModel *)model{
    _model = model;
    [self.tableView reloadData];
    self.tableView.tableFooterView = self.gradeView;
     self.gradeView.model = self.gradeModel;
}

- (void)setClickNumberString:(NSString *)clickNumberString{
    _clickNumberString = clickNumberString;
    [self.clickNumber setTitle:[NSString stringWithFormat:@" 点击量 %@",clickNumberString] forState:UIControlStateNormal];
}

#pragma mark - Getters

- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 100) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;

        [_tableView  registerClass:[ZJSCSynopsisTableViewCell class] forCellReuseIdentifier: CellID];
        [_tableView registerClass:[ZJSCSynopsisSectionView class] forHeaderFooterViewReuseIdentifier: SectionID];
        _tableView.scrollEnabled = NO;
        
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 30 * [UIScreen zj_screenHScale])];
        self.clickNumber = [[UIButton alloc] initWithFrame: CGRectMake(12 * [UIScreen zj_screenWScale], 0, 300 * [UIScreen zj_screenWScale], headerView.height)];
        [self.clickNumber setImage:[UIImage imageNamed:@"icon_click"] forState:UIControlStateNormal];
        [self.clickNumber setTitle:@" 点击量 0" forState:UIControlStateNormal];
        [self.clickNumber setTitleColor:[UIColor zj_colorWithHexString:@"999999"] forState:UIControlStateNormal];
        self.clickNumber.titleLabel.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
        self.clickNumber.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [headerView addSubview:self.clickNumber];
        _tableView.tableHeaderView = headerView;
        
        
    }
    return _tableView;
}
- (ZJCourseGradeView *)gradeView{
    if (_gradeView == nil){
        _gradeView = [[ZJCourseGradeView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 44 * [UIScreen zj_screenHScale])];
        _gradeView.delegate = self;
    }
    return _gradeView;
}

@end
